import {toast, alert, confirm} from 'amis';
import {request} from "@umijs/max";
import copy from 'copy-to-clipboard';

export default function amisEnvModel() {

  return {
    fetcher: ({url, method, data, config, headers}: any) => {
      console.log("fetcher------------>",url,method,data,config,headers)
      config.method = method
      const configAssign = config || {};
      configAssign.headers = config.headers || headers || {};
      configAssign.withCredentials = true;

      if (method !== 'post' && method !== 'put' && method !== 'patch') {
        if (data) {
          configAssign.params = data;
        }
        if(url.startsWith("http")){
          return request<any>(url, {
            method: method,
            ...configAssign
          })
        }else{
          return request<any>('/wmsServer'+url, {
            method: method,
            ...configAssign
          })
        }

      } else{
        configAssign.data = data
      }
      console.log("fetch 请求--------------",method)
      if(url.startsWith("http")){
        return request<any>(url,{
          method:method,
          ...configAssign
        })
      }else{
        return request<any>('/wmsServer'+url,{
          method:method,
          ...configAssign
        })
      }

    },
    notify: (type: 'success' | 'error' | 'info' | 'warning', msg: string) => {
      toast[type](msg, type === 'error' ? '系统错误' : '系统消息')
    },
    alert,
    confirm,
    copy: (contents: string, options: any = {}) => {
      const ret = copy(contents, options);
      if(ret && (!options || options.shutup !== true)){
        toast.info('内容已拷贝到剪切板');
      }
      return ret;
    }
  }
}
